<?php
	set_include_path (PHPPATH);
	require_once 'MDB2.php';
	/**
	 * 
	 * The product controller
	 * @author Tri
	 *
	 */
	class product extends Application 
	{
		/**
		 * 
		 * Constructor
		 */
		function __construct(&$tmpl)
		{			
			$this->tmpl = &$tmpl;		
		}

		/**
		 * 
		 * Default action
		 */
		function index()
		{	
            // get current and back view
			$currentView = $_REQUEST['view'];
			$this->assign("currentView", $currentView);
			$backTitle = $_REQUEST['back'];
			$this->assign("backTitle", $backTitle);
            
            $category_id = $_REQUEST['catg'];
            $categoryModel = $this->getModel('CategoryModel');
            $category = $categoryModel->getCategoryById($category_id);
            if($category == NULL) 
                return;
            
            $this->assign("title", $category['name']);
            $this->assign("page", "catg");
            $this->assign("pageId", $category['id']);
            
            $productModel = $this->getModel('ProductModel');
            $rowProduct = $productModel->getAllProductByCategory($category['id']);
            $this->assign("products", $rowProduct);
            $this->assign("products_count", count($rowProduct));
			
            $this->assign("imagesPath", $this->loadResources('images'));
            $this->assign("productPath", $this->loadResources('product'));
			$this->loadPageTemplate('product_list_page');
		}
        
        function details() // chi co hinh
        {    
            // get current and back view, index
            $userId = $this->getLoggedUser();
			if($userId == 0) {
				$this->assign("loginHeartVisible", 1);
			}
            $this->assign("uid", $userId);
            $currentView = $_REQUEST['view'];
            $this->assign("currentView", $currentView);
            $backTitle = $_REQUEST['back'];
            $this->assign("backTitle", $backTitle);
            $index = $_REQUEST['index'];
            $this->assign('index', $index);
			$parentId = $_REQUEST['parentId'];
            $this->assign("parentId", $parentId);
            
            $productModel = $this->getModel('ProductModel');
			//sf = search from (name, brand, store)
            //kw = keyword            
            if (isset($_REQUEST['sf']) && $_REQUEST['sf'] != "" && isset($_REQUEST['kw']) && $_REQUEST['kw'] != ""){
            	$txtSearch = $_REQUEST['kw'];
            	$sf = $_REQUEST['sf'];
            	
            	switch ($sf){
            		case 'name':
            			$rowProduct = $productModel->getAllProductSearchByName($txtSearch);
            			break;
            		case 'brand':
            			$rowProduct = $productModel->getAllProductSearchByBrand($txtSearch);
            			break;
            		case 'store':
            			$rowProduct = $productModel->getAllProductSearchByStore($txtSearch);
            			break;
            	}
            	$title ="Result";
		        $page = "list-".$parentId;
		        $pageId = "";
            }
            else {
	            switch($currentView){
	                case 'feature':
	                    $rowProduct = $productModel->getFeatureProducts();
	                    $title = 'Feature';
	                    $page = "list-".$parentId;
		                $pageId = "";
	                break;
	                
	                case 'favorite':
	                	$rowProduct = $productModel->getFavProductsByUserId($userId);
	                    $title = 'Details';
	                    $page = "list-".$parentId;
		                $pageId = "";
	                break;
	                
	                case 'sales':
	                    $rowProduct = $productModel->getSalesProducts();
	                    $title = 'Sales';
	                    $page = "list-".$parentId;
		                $pageId = "";
	                break;
	                
	                case 'home':
	                	if (isset($_REQUEST['catg']) && $_REQUEST['catg'] != ""){
		                    $category_id = $_REQUEST['catg'];
		                    $categoryModel = $this->getModel('CategoryModel');
		                    $category = $categoryModel->getCategoryById($category_id);
		                    $rowProduct = $productModel->getAllProductByCategory($category['id']);
		                    $title = $category['name'];
		                    $page = "list-".$parentId;
		                    $pageId = "";
	                	}
	                	else if (isset($_REQUEST['type']) && $_REQUEST['type'] != ""){
	                		$typeId = $_REQUEST['type'];
							$typeModel = $this->getModel('TypeModel');
							$type = $typeModel->getTypeById($typeId);
							if ($type == null)
								return;
							$productModel = $this->getModel('ProductModel');
				            $rowProduct = $productModel->getProductsByQuery($type['query']);
				            
				            $title = $type['name'];
		                    $page = "list-".$parentId;
		                    $pageId = "";
				        }
				        break;
	            }
            }
            
       		if ($userId != 0){
         		$this->assign("viewFavVisible", 1);
         		$favProducts = $productModel->getFavProductsByUserId($userId);
         		$favArrayProductId = array();
         		if (count($favProducts) > 0){
	         		foreach ($favProducts as $k => $v){
	         			$favArrayProductId[] = $v['id'];
	         		}
	         		foreach ($rowProduct as $k => $v){
	         			if(in_array($v['id'], $favArrayProductId)){
	         				$rowProduct[$k]['fav'] = "remove";
	         			}
	         			else{
	         				$rowProduct[$k]['fav'] = "add";
	         			}
	         		}
         		}
         		else{
	         		foreach ($rowProduct as $k => $v){
		         		$rowProduct[$k]['fav'] = "add";
	         		}
         		}
            }
           
            $this->assign("title", $title);
            $this->assign("page", $page);
            $this->assign("pageId", $pageId);
            
            $this->assign("products", $rowProduct);
            $this->assign("products_count", count($rowProduct));
            
            $this->assign("imagesPath", $this->loadResources('images'));
            $this->assign("productPath", $this->loadResources('product'));
            
            $this->loadPageTemplate('product_details_page');
        }
        
        function view() // co chi detail
        {    
            // get current and back view
            $userId = $this->getLoggedUser();
       		if($userId == 0) {
				$this->assign("loginHeartVisible", 1);
			}
            $this->assign("uid", $userId);
            $currentView = $_REQUEST['view'];
            $this->assign("currentView", $currentView);
            $this->assign("backTitle", 'Back');
            $parentId = $_REQUEST['parentId'];
            $this->assign("parentId", $parentId);
            
            $product_id = $_REQUEST['pro'];
            
            $productModel = $this->getModel('ProductModel');
            $rowProduct = $productModel->getProductById($product_id);
            $this->assign("product", $rowProduct);
            
            $price_discount = (((100 - $rowProduct['discount'])*$rowProduct['price'])/100);
            $start_date = substr($rowProduct['start_date'], 0, 10);
            $end_date = substr($rowProduct['end_date'], 0, 10);
            $curdate = time();

            if ($curdate >= strtotime($start_date) && $curdate <= strtotime($end_date) ) {
                $this->assign("isprice", 1);
                $this->assign("discount", number_format($price_discount));
                $this->assign("price", number_format($rowProduct['price']));
            }else{
                $this->assign("isprice", 0);
            }
            
            if ($userId != 0){
         		$this->assign("detailFavVisible", 1);
         		$favProducts = $productModel->getFavProductsByUserId($userId);
         		$favArrayProductId = array();
         		if (count($favProducts) > 0){
         			foreach ($favProducts as $k => $v){
         				if ($v['id'] == $product_id)
         					$this->assign("favProduct", 1);
         			}
         		}
            }
            
            $this->assign("pid", $rowProduct['id']);
            
            $this->assign("title", $rowProduct['name']);
            $this->assign("page", "proview");
            $this->assign("pageId", $rowProduct['id']);
            
            $this->assign("imagesPath", $this->loadResources('images'));
            $this->assign("productPath", $this->loadResources('product'));
            
            $this->loadPageTemplate('product_view');
        }
	}
?>